package com.ruipengkj.component.jtree;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class JTreeCellRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 1L;

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

//		JTreeNode node = (JTreeNode) value;
//		Icon icon = node.getIcon();
		ImageIcon imageOpen = new ImageIcon("images/tree_open.gif");
		ImageIcon imageClose = new ImageIcon("images/tree_close.gif");
		// 设置用于表示叶节点的图标
		this.setLeafIcon(imageOpen);
		// 设置用于表示扩展的非叶节点的图标
		this.setOpenIcon(imageOpen);
		// 设置用于表示没有扩展的非叶节点的图标。
		this.setClosedIcon(imageClose);
		// 配置基于传入组件的渲染器。使用 convertValueToText 向树传递消息，
		// 从而设置该值，最终对 value 调用 toString。基于选择设置前景色，基于 leaf 和 expanded 参数设置图标。
		return super.getTreeCellRendererComponent(tree, value, sel, expanded,
				leaf, row, hasFocus);
	}
}
